package com.road.vast.system.permission.controller;


import com.road.vast.common.entity.Result;
import com.road.vast.common.entity.ResultCode;
import com.road.vast.common.exception.CommonException;
import com.road.vast.pojo.system.PePermission;
import com.road.vast.system.permission.service.PePermissionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限信息 前端控制器
 * </p>
 *
 * @author 郭泽鹏
 * @since 2020-07-17
 */
@RestController
@RequestMapping("/permission")
public class PePermissionController {
    @Resource
    private PePermissionService pePermissionService;

    /**
     * 保存权限信息
     */
    @PostMapping("")
    public Result savePermission(@RequestBody Map<String, Object> map) throws Exception {
        pePermissionService.savePermission(map);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 更改权限信息
     */
    @PutMapping("/{id}")
    public Result updatePermission(@PathVariable(value = "id") String id,
                                   @RequestBody Map<String, Object> map) throws Exception {
        map.put("id", id);
        pePermissionService.updatePermission(map);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 查询所有权限信息
     */
    @GetMapping("")
    public Result getAll(@RequestParam Map<String, Object> map){
        List<PePermission> all = pePermissionService.getAll(map);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(all);
        return result;
    }

    /**
     * 通过id进行查询
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable(value = "id") String id) throws CommonException {
        Map<String, Object> map = pePermissionService.getById(id);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(map);
        return result;
    }

    /**
     * 通过id删除权限信息
     */
    @DeleteMapping("/{id}")
    public Result deletePermission(@PathVariable(value = "id") String id) throws CommonException {
        pePermissionService.deletePermission(id);
        return new Result(ResultCode.SUCCESS);
    }
}

